package com.net.xpay.openapi.controller.openapi.common;

import com.google.common.base.Preconditions;
import com.net.xpay.openapi.aop.OpenApi;
import com.net.xpay.openapi.dto.OpenApiOpenImageDto;
import com.net.xpay.openapi.protocol.OpenApiBase;
import com.net.xpay.openapi.protocol.OpenApiProtocol;
import com.net.xpay.openapi.protocol.OpenApiResult;
import com.net.xpay.openapi.protocol.OpenApiSignature;
import com.net.xpay.common.annotation.ApiMonitoring;
import com.net.xpay.common.domain.openapi.OpenApp;
import com.net.xpay.common.domain.openapi.OpenImage;
import com.net.xpay.common.manager.openapi.OpenImageManager;
import com.net.xpay.common.service.ImageService;
import com.net.xpay.core.utils.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理单文件或者多文件上传
 */
@RestController
@RequestMapping("/apiv1/openapi/open-image")
@Api(tags = "第三方开放调用/图片上传", description = "第三方开放调用 图片上传 net")
public class OpenApiOpenImageController {
    @Autowired
    private ImageService imageService;

    @Autowired
    private OpenApiBase openApiBase;

    @Autowired
    private OpenImageManager openImageManager;

    @OpenApi
    @ApiMonitoring(value = "图片/上传图片")
    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public OpenApiResult<OpenApiOpenImageDto> saveImage(HttpServletRequest request,
                                                        @RequestParam("appId") String appId,
                                                        @RequestParam("timestamp") String timestamp,
                                                        @RequestParam("sign") String sign,
                                                        @RequestParam("bizContent") String bizContent, //文件名
                                                        @RequestParam(value = "file", required = false) MultipartFile file) {
        Preconditions.checkArgument(file != null);
        OpenApiProtocol openApiProtocol = new OpenApiProtocol();
        openApiProtocol.setAppId(appId);
        openApiProtocol.setTimestamp(timestamp);
        openApiProtocol.setSign(sign);
        openApiProtocol.setBizContent(bizContent);
        OpenApp openApp = openApiBase.checkProtocol(request, openApiProtocol);
        String uuid = UuidUtil.uuid();
        String url = imageService.commonSaveImage(uuid, file);

        OpenApiOpenImageDto dto = new OpenApiOpenImageDto();
        dto.setImageId(uuid);
        OpenImage openImage = new OpenImage();
        openImage.setImageId(openImage.getImageId());
        openImage.setUserId(openApp.getUserId());
        openImage.setAppId(openApp.getId());
        openImage.setImageUrl(url);
        openImageManager.save(openImage);

        OpenApiResult<OpenApiOpenImageDto> openApiResult = new OpenApiResult<>(dto);
        Map<String, String> params = new HashMap<>();
        params.put("resultCode", openApiResult.getResultCode());
        params.put("resultMessage", openApiResult.getResultMessage());
        String signCheckContent = OpenApiSignature.getSignCheckContent(params);
        String rsaSign = OpenApiSignature.rsaSign(signCheckContent, openApp.getPrivateKey(), "UTF-8", "RSA2");
        openApiResult.setSign(rsaSign);

        return openApiResult;
    }

}
